package com.d3works.apps.base.tracking;

import com.d3works.apps.base.db.GlobalTrackingLog;
import com.d3works.exception.service.TraceBackService;
import com.d3works.inject.Beans;
import com.d3works.meta.schema.actions.ActionView;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.d3works.rpc.Context;

public class GlobalTrackingLogController {

  public void showGlobalTrackingLogsInWizard(ActionRequest request, ActionResponse response) {
    try {

      Context context = request.getContext();
      boolean showLines = context.get("metaModel") != null && context.get("metaField") != null;

      response.setAttr("globalTrackingLogDashlet", "hidden", showLines);
      response.setAttr("globalTrackingLogLineDashlet", "hidden", !showLines);

      response.setAttr(
          showLines ? "globalTrackingLogLineDashlet" : "globalTrackingLogDashlet", "refresh", true);
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }

  public void showReference(ActionRequest request, ActionResponse response) {
    try {
      GlobalTrackingLog globalTrackingLog = request.getContext().asType(GlobalTrackingLog.class);

      ActionView.ActionViewBuilder actionViewBuilder =
          Beans.get(GlobalTrackingLogService.class).createReferenceView(globalTrackingLog);

      if (actionViewBuilder != null) {
        response.setView(actionViewBuilder.map());
      }
    } catch (Exception e) {
      TraceBackService.trace(response, e);
    }
  }
}
